﻿using CodeCity.SDK;
using CodeCity.UserManagement.Model;
using CodeCity.UserManagement.Service.ApplicationModel;
using CodeCity.UserMangament.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeCity.UserManagement.Service
{
    public class HouseService
    {
        private readonly HouseRepository houseRepository;

        public HouseService(HouseRepository houseRepository)
        {
            this.houseRepository = houseRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task RegisterAsync(HouseRegisterModel model)
        {
            if (await houseRepository.ExistAsync(model.Id))
            {
                throw new OperationException("该住房已注册");
            }
            houseRepository.Insert(new House()
            {
                BuildingTime = model.BuildingTime,
                UnitId = model.Id,
                MaxHumen = model.MaxHumen,
                Humen = model.Humen,
                IsUnderConstruction = model.IsUnderConstruction,
                Position = model.Position
            });
            await houseRepository.SaveChangesAsync();
        }
        public async Task CompleteAsync(BuildingCompleteModel model)
        {
            var hosiptal = await houseRepository.GetByUnitIdAsync(model.Id);
            if (hosiptal == null)
            {
                throw new OperationException("住房不存在");
            }
            hosiptal.IsUnderConstruction = false;
            hosiptal.CompleteTime = model.CompleteTime;
            await houseRepository.SaveChangesAsync();
        }

        public Task<List<House>> LoadAllAsync()
        {
            return houseRepository.GetAllAsync();
        }
    }
}
